﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wrsm.wow;
using wrsm.wow.entity;

namespace wrsm.client {

    /// <summary>
    /// Manages World of Warcraft Installations
    /// </summary>
    internal class WowInstallationManager {

        #region [Events]

        /// <summary>
        /// Event indicating that a Wow Installation has been added, or has changed
        /// </summary>
        public event WowInstallationHandler InstallationAddedOrModified;

        /// <summary>
        /// Event indicating that a Wow Installation is no longer managed
        /// </summary>
        public event WowInstallationHandler InstallationRemoved;

        #endregion

        #region [Local Attributes]

        /// <summary>
        /// Wow Installation utility
        /// </summary>
        private WowInstallationUtility _wowInstallationUtility;

        /// <summary>
        /// Handles loading and saving information regarding Wow Installations
        /// </summary>
        private WowInstallationFileTransformer _wowInstallationFileTransformer;

        /// <summary>
        /// A collection of managed installations
        /// </summary>
        private Dictionary<string, WowInstallation> _managedInstallations;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Wow Installation Manager
        /// </summary>
        public WowInstallationManager() {
            //Wireup Internal Event Handlers
            WireupEvents();
            _wowInstallationUtility = new WowInstallationUtility();
            _managedInstallations = new Dictionary<string, WowInstallation>();
            //TODO: Add some functionality that will allow this information to be loaded up with the application
        }

        #endregion

        #region [The guts]

        /// <summary>
        /// Returns reference to all managed wow installations
        /// </summary>
        /// <returns>array of wow installations</returns>
        public WowInstallation[] GetAllManagedInstallations() {
            return _managedInstallations.Values.ToArray();
        }

        /// <summary>
        /// Add a World of Warcraft Installation to manager
        /// </summary>
        /// <param name="install">Installation to add to the manager</param>
        public void AddInstall(WowInstallation install) {
            //Update the wow isntallation valid flag, while testing if the flag has changed.
            bool validInstallChanged = (install.ValidInstall == ValidateWowInstallation(install));
            //Check if this is a new installation
            bool newInstallation = !_managedInstallations.ContainsKey(install.InstallLocation);
            //Update our current refernce to the install
            _managedInstallations[install.InstallLocation] = install;
            if (validInstallChanged || newInstallation) {
                InstallationAddedOrModified(install);
            }
        }

        /// <summary>
        /// Remove a Wow Installation from the manager
        /// </summary>
        /// <param name="install">installation to remove from the manager</param>
        public void RemoveInstall(WowInstallation install) {
            if (_managedInstallations.ContainsKey(install.InstallLocation))
                _managedInstallations.Remove(install.InstallLocation);
            InstallationRemoved(install);
        }

        /// <summary>
        /// Validates a world of warcraft Installation
        /// </summary>
        /// <param name="install">world of warcraft installation to validate</param>
        private bool ValidateWowInstallation(WowInstallation install) {
            return install.ValidInstall = _wowInstallationUtility.IsValidWowInstall(install);
        }

        #endregion

        #region [Events and Wireup]

        /// <summary>
        /// Just wireup events to internal handlers
        /// </summary>
        private void WireupEvents() {
            InstallationAddedOrModified += new WowInstallationHandler(OnInstallationAddedOrModified);
            InstallationRemoved += new WowInstallationHandler(OnInstallationRemoved);
        }

        /// <summary>
        /// Handles InstallationAddedOrModified Event, internally
        /// </summary>
        /// <param name="install">Wow Installation</param>
        private void OnInstallationAddedOrModified(WowInstallation install) {
        }

        /// <summary>
        /// Handles InstallationRemoved Event, internally
        /// </summary>
        /// <param name="install">Wow Installation</param>
        private void OnInstallationRemoved(WowInstallation install) {
        }

        #endregion

    }
}
